<?php
	class ShortMessageAction extends CommonAction{
		public function _filter(&$map){
			$map['receivername'] = $_SESSION['loginUserName'];
			$map['deleted']=0;
			$map['content'] = array('like',"%".$_POST['loginUserName']."%");
		}
		public function _before_index() {
			$model = M ( "ShortMessage" );
	   		$order='sendtime';
	   		$sort='desc';
	   		$_REQUEST['_order']=$order;
	   		$_REQUEST['_sort']=$sort;
		}
		public function _before_add() {
			$model=M('User');
			$list=$model->where('username!='."'".$_SESSION['username']."'")->select();
			$this->assign('list',$list);
			$this->assign('currentPage',$_REQUEST['pageNum']);
		}
		public function readmsg(){
			$model=M('ShortMessage');
			$id=$_REQUEST['ids'];
			$this->vo = $model->find($id);
			$name=$model->where('id='.$id)->getField('sendername');
			$_SESSION['replyname']=$name;
			$model->where('id='.$id)->setField('state','1');
			$data['readtime']=date('Y-n-j H:i:s');
			$result=$model->where('id='.$id)->save($data);
			
			$this->assign('currentPage',$_REQUEST['pageNum']);
			
			$this->display();
		}
		public function outbox(){
			$model=M('ShortMessage');
			$sendername=$_SESSION['loginUserName'];
			import ( '@.Org.Page' );
			$count = $model->where("sendername='".$sendername."'")->count ();
			$page = new Page ( $count, ! empty ( $_REQUEST ['numPerPage'] ) ? $_REQUEST ['numPerPage'] : 10 );
			$pageNum = ! empty ( $_REQUEST ['pageNum'] ) ? $_REQUEST ['pageNum'] : 1;
			$list = $model->where("sendername='".$sendername."'")->page ( $pageNum . "," . $page->listRows )->order('sendtime desc')->select ();
						
			$page->setConfig('header','条');
			$page->setConfig('first','首页');
			$page->setConfig('last','尾页');
			$page->setConfig('theme', "<div data-role='button' data-theme='a'>共%totalRow%  %header%  第%nowPage%/%totalPage% 页   %upPage% %downPage% %first%    %linkPage%   %end%</div>");
			//分页显示
			$page = $page->show ();
			//如果信息过长，则简略显示
			foreach ( $list as $key => $value ) {
				$length = strlen ( $value ['content'] );
				if ($length > 18) {
					$value ['content'] = substr ( $value ['content'], 0, 15 ) . '......';
					$list [$key] = $value;
				}
			}
			
			$this->assign('list',$list);
			$this->assign ( 'totalCount', $count ); // 总条数
			$this->assign ( 'numPerPage', $page->listRows ); // 每页显示的条数
			$this->assign ( 'currentPage', $pageNum ); // 当前页
			$this->assign('page',$page);
			$this->display();
		}
		public function Check(){
			$id=$_REQUEST['ids'];
			$model = M('ShortMessage');
			$this->vo = $model->find($id);
			
			$this->assign('currentPage',$_REQUEST['pageNum']);
			
			$this->display();
		}
	}
?>